<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class ArchivesAbility extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'archives_ability';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'exam_time_text',
        'exam_status_text'
    ];

    public function getLevelList()
    {
        return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3'), '4' => __('Level 4')];
    }

    public function getExamStatusList()
    {
        return ['1' => __('Exam_status 1'), '2' => __('Exam_status 2'), '3' => __('Exam_status 3')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getLevelList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getExamTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['exam_time']) ? $data['exam_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getExamStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['exam_status']) ? $data['exam_status'] : '');
        $list = $this->getExamStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setExamTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function archives()
    {
        return $this->belongsTo('Archives', 'archives_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('Admin', 'exam_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
